package handler

import (
	"day1/video/common"
	"fmt"
	"net/http"

	"day1/video/api/internal/logic"
	"day1/video/api/internal/svc"
	"day1/video/api/internal/types"
	"github.com/zeromicro/go-zero/rest/httpx"
)

func UpdateHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.Updaterequest
		f, h, e := r.FormFile("path")
		if e != nil {
			fmt.Println("文件接受失败")
			return
		}
		defer f.Close()
		path := common.AliFile(h)
		r.Form.Set("path", path)
		r.Form.Set("name", h.Filename)
		fmt.Println(path)
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := logic.NewUpdateLogic(r.Context(), svcCtx)
		resp, err := l.Update(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
